package cn.jekun.shop.web.action;

import java.util.Date;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.DateUtils;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.MyCreditsService;
import cn.jekun.shop.common.service.ShopBaseAction;

public class MyCreditsAction extends ShopBaseAction{
	
	@Resource(name=MyCreditsService.BEAN_NAME)
	MyCreditsService myCreditsService;
	
	public String creditsJournal(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String userId = this.getUserId();
			String operationTypeId = this.request.getParameter("operationTypeId");
			String days = this.request.getParameter("days");//<=天数
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			Date endDate = null;
			try {
				int d = Integer.parseInt(days);
				endDate = DateUtils.addDays(new Date(System.currentTimeMillis()), d*-1);//
			} catch (Exception e) {
			}
			jsonObject = myCreditsService.getMyCredits(userId,operationTypeId,endDate,page);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
}
